package com.cib.record.web;

import com.cib.record.bean.Audio;
import com.cib.record.mapper.AudioMapper;
import com.cib.record.service.UploadService;
import com.cib.record.util.ShellUtil;
import org.omg.PortableInterceptor.SUCCESSFUL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @描述:
 * @Author: zhangh
 * @Time: 2021/5/2 20:21
 **/
@Controller
public class UploadController {
    @Value("${tar.path}")
    private String tar_path;

    //供edip方curl
//    @Value("${tar.gz.path}")
//    private String gzpath;

    @Autowired
    UploadService uploadService;

    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);


    /**
     * ASR==>>统一归集服务器的重传
     * 此方法提供目标服务器来curl,如果MD5校验失败，curl此接口重传
     * @return
     */
    @GetMapping("retrans")
    @ResponseBody
    public String retrans(String tarname) {
        logger.info("enter retrans function...");
        try {
            String filename = tar_path + tarname;
            logger.info("retrans filename is "+filename);
            uploadService.uploadFile(tar_path+tarname);
        }catch (Exception e){
            logger.error("retrans failed!");
            return "retrans failed!";
        }

        logger.info("retrans success!");
        return "success";

    }


    /**
     * 这个重传方法是针对归集服务器的，当edip平台MD5校验失败以后，可以curl这个方法进行重传
     * @param
     * @return
     */
   /* @GetMapping("reupload")
    @ResponseBody
    public String reupload(String tarname) {
        logger.info("enter reupload function...");
        try {
            String filename = gzpath + tarname;
            logger.info("reupload filename is "+filename);
            uploadService.uploadFile(tar_path+tarname);
        }catch (Exception e){
            logger.error("retrans failed!");
            return "retrans failed!";
        }

        logger.info("retrans success!");
        return "success";

    }*/

    @GetMapping("hello")
    @ResponseBody
    public String hello() throws UnknownHostException {
        //uploadService.uploadFile("helo");
       /* Audio audio = audioMapper.selectByPrimaryKey(1);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String today = simpleDateFormat.format(date);
        List<Audio> audiolist=audioMapper.selectTodayAudio(today);
        System.out.println(audiolist);
        System.out.println(audio);*/

        String hostAddress = InetAddress.getLocalHost().getHostAddress();

        return hostAddress;
    }
}
